package com.macro.mall.tiny.modules.ums.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.modules.ums.model.MeetingRoom;
import com.macro.mall.tiny.modules.ums.mapper.MeetingRoomMapper;
import com.macro.mall.tiny.modules.ums.model.UmsRole;
import com.macro.mall.tiny.modules.ums.service.MeetingRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会议室表 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-04-11
 */
@Service
public class MeetingRoomServiceImpl extends ServiceImpl<MeetingRoomMapper, MeetingRoom> implements MeetingRoomService {

    @Override
    public Page<MeetingRoom> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<MeetingRoom> page = new Page<>(pageNum,pageSize);
        QueryWrapper<MeetingRoom> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<MeetingRoom> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(keyword)){
            lambda.like(MeetingRoom::getName,keyword);
        }
        return page(page,wrapper);
    }
    @Override
    public boolean updateByMeetingId(MeetingRoom meetingRoom) {
        MeetingRoom m = getById(meetingRoom.getId());
        if (!StrUtil.equals(m.getName(), meetingRoom.getName()) && getMeetingRoomByName(meetingRoom.getName()) != null) {
            return false;
        }
        return updateById(meetingRoom);
    }

    @Override
    public boolean create(MeetingRoom meetingRoom) {
        if (getMeetingRoomByName(meetingRoom.getName()) != null) {
            return false;
        }
        meetingRoom.setCreateTime(new Date());
        return save(meetingRoom);
    }
    @Override
    public boolean delete(Long id) {
        boolean success = removeById(id);
        return success;
    }

    private MeetingRoom getMeetingRoomByName(String name) {
        QueryWrapper<MeetingRoom> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<MeetingRoom> lambda = wrapper.lambda();
        lambda.eq(MeetingRoom::getName,name);
        MeetingRoom meetingRoom = getOne(lambda);
        return meetingRoom;
    }
}
